﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TravelLink.DAL;

namespace TravelLink.WebApp.UserControl.Member.JobEditable {



    public class TL_User_CareerExperience_Ext {

        public string Achievement { get; set; }
        public string CompanyAddress { get; set; }
        public string CompanyName { get; set; }
        public string CompanySite { get; set; }
        public string ContactMobile { get; set; }
        public string ContactName { get; set; }
        public string ContacttMail { get; set; }
        public string FromYear { get; set; }
        //ToYear = !u.Item.ToYear.HasValue ? "至今" : u.Item.ToYear.Value.ToString("yyyy-MM"),
        public string ToYear { get; set; }
        public int Idx { get; set; }
        public string JobDesc { get; set; }
        public string LeaveReason { get; set; }
        public string Position { get; set; }
        public string ReportTo { get; set; }
        public int? ResumeId { get; set; }
        public DateTime? RowTime { get; set; }
    }





    public class JobEditableCarrer : JobEditableBase {

        public JobEditableCarrer(bool isEn) : base(isEn) { }

        public override string AspGridViewName {
            get { return "aspxGridCareer"; }
        }

        protected override IEnumerable<object> GetSource(ResumeParam p) {
            if (Has(AspGridViewName)) {
                var memoryList = Get<TL_User_CareerExperience>(u => u.Status != EditableItemStatus.Delete);
                if (memoryList != null) {
                    return memoryList.Select(u => new TL_User_CareerExperience_Ext {
                        Achievement = u.Item.Achievement,
                        CompanyAddress = u.Item.CompanyAddress,
                        CompanyName = u.Item.CompanyName,
                        CompanySite = u.Item.CompanySite,
                        ContactMobile = u.Item.ContactMobile,
                        ContactName = u.Item.ContactName,
                        ContacttMail = u.Item.ContacttMail,
                        FromYear = u.Item.FromYear.Value.ToString("yyyy-MM"),
                        //ToYear = !u.Item.ToYear.HasValue ? "至今" : u.Item.ToYear.Value.ToString("yyyy-MM"),
                        ToYear = u.Item.ToYear.HasValue ? u.Item.ToYear.Value.ToString("yyyy-MM") : null,
                        Idx = u.Item.Idx,
                        JobDesc = u.Item.JobDesc,
                        LeaveReason = u.Item.LeaveReason,
                        Position = u.Item.Position,
                        ReportTo = u.Item.ReportTo,
                        ResumeId = u.Item.ResumeId,
                        RowTime = u.Item.RowTime
                        //u.Item.ToYear
                    }).ToList();
                }
                return null;
            }
            else {
                var db = DataContextFactory.Create();
                List<EditableItem<TL_User_CareerExperience>> innerList = db.TL_User_CareerExperience
                    .Where(u => u.ResumeId == p.ResumeIdx)
                    .Select(u => Add<TL_User_CareerExperience>(u, EditableItemStatus.Default))
                    .ToList();
                Set(AspGridViewName, innerList);
                return innerList.Select(u => new TL_User_CareerExperience_Ext {
                    Achievement = u.Item.Achievement,
                    CompanyAddress = u.Item.CompanyAddress,
                    CompanyName = u.Item.CompanyName,
                    CompanySite = u.Item.CompanySite,
                    ContactMobile = u.Item.ContactMobile,
                    ContactName = u.Item.ContactName,
                    ContacttMail = u.Item.ContacttMail,
                    FromYear = u.Item.FromYear.Value.ToString("yyyy-MM"),
                    //ToYear = !u.Item.ToYear.HasValue ? "至今" : u.Item.ToYear.Value.ToString("yyyy-MM"),
                    ToYear = u.Item.ToYear.HasValue ? u.Item.ToYear.Value.ToString("yyyy-MM") : null,
                    Idx = u.Item.Idx,
                    JobDesc = u.Item.JobDesc,
                    LeaveReason = u.Item.LeaveReason,
                    Position = u.Item.Position,
                    ReportTo = u.Item.ReportTo,
                    ResumeId = u.Item.ResumeId,
                    RowTime = u.Item.RowTime
                }).ToList();
            }
        }

        public override void Save(ResumeParam p) {
            var db = DataContextFactory.Create();

            var memoryList = Get<TL_User_CareerExperience>(u => true);
            var insetedList = memoryList.Where(u => u.Status == EditableItemStatus.Insert);
            foreach (var item in insetedList) {
                db.TL_User_CareerExperience.InsertOnSubmit(new TL_User_CareerExperience {
                    Achievement = item.Item.Achievement,
                    CompanyAddress = item.Item.CompanyAddress,
                    CompanyName = item.Item.CompanyName,
                    CompanySite = item.Item.CompanySite,
                    ContactMobile = item.Item.ContactMobile,
                    ContactName = item.Item.ContactName,
                    ContacttMail = item.Item.ContacttMail,
                    FromYear = item.Item.FromYear,
                    JobDesc = item.Item.JobDesc,
                    LeaveReason = item.Item.LeaveReason,
                    Position = item.Item.Position,
                    ReportTo = item.Item.ReportTo,
                    ToYear = item.Item.ToYear,
                    ResumeId = item.Item.ResumeId,
                    RowTime = item.Item.RowTime
                });
            }

            var updateList = memoryList.Where(u => u.Status == EditableItemStatus.Edit);
            foreach (var item in updateList) {
                var skillEntity = db.TL_User_CareerExperience.FirstOrDefault(u => u.Idx == item.Item.Idx);
                if (skillEntity != null) {

                    skillEntity.CompanyAddress = item.Item.CompanyAddress;
                    skillEntity.CompanyName = item.Item.CompanyName;
                    skillEntity.CompanySite = item.Item.CompanySite;
                    skillEntity.ContactMobile = item.Item.ContactMobile;
                    skillEntity.ContactName = item.Item.ContactName;
                    skillEntity.ContacttMail = item.Item.ContacttMail;
                    skillEntity.FromYear = item.Item.FromYear;
                    skillEntity.JobDesc = item.Item.JobDesc;
                    skillEntity.LeaveReason = item.Item.LeaveReason;
                    skillEntity.Position = item.Item.Position;
                    skillEntity.ReportTo = item.Item.ReportTo;
                    skillEntity.ToYear = item.Item.ToYear;
                    skillEntity.ResumeId = item.Item.ResumeId;
                    skillEntity.Achievement = item.Item.Achievement;
                    skillEntity.RowTime = item.Item.RowTime;
                }
            }

            var deleteList = memoryList.Where(u => u.Status == EditableItemStatus.Delete);
            foreach (var item in deleteList) {
                db.TL_User_CareerExperience.DeleteAllOnSubmit(db.TL_User_CareerExperience.Where(u => u.Idx == item.Item.Idx));
            }

            db.SubmitChanges();

            Clear(AspGridViewName);
        }

        protected override void PreDeleting(ResumeParam p, DevExpress.Web.Data.ASPxDataDeletingEventArgs e, DevExpress.Web.ASPxGridView.ASPxGridView sender) {
            var memoryList = Get<TL_User_CareerExperience>(u => true);

            if (memoryList == null) return;

            int key = ((int)e.Keys[0]);

            var entity = memoryList.Where(u => u.Item.Idx == key).FirstOrDefault();
            if (entity.Status != EditableItemStatus.Insert)
                entity.Status = EditableItemStatus.Delete;
            else {
                memoryList.Remove(entity);
            }

            Set(AspGridViewName, memoryList);
        }

        protected override void PreUpdating(ResumeParam p, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e, DevExpress.Web.ASPxGridView.ASPxGridView sender) {
            var memoryList = Get<TL_User_CareerExperience>(u => true);

            if (memoryList == null) return;

            int key = ((int)e.Keys[0]);
            string _CompanyName = e.NewValues["CompanyName"] == null ? string.Empty : e.NewValues["CompanyName"].ToString();
            string _Position = e.NewValues["Position"] == null ? string.Empty : e.NewValues["Position"].ToString();
            string _ReportTo = e.NewValues["ReportTo"] == null ? string.Empty : e.NewValues["ReportTo"].ToString();
            string _CompanyAddress = e.NewValues["CompanyAddress"] == null ? string.Empty : e.NewValues["CompanyAddress"].ToString();
            string _JobDesc = e.NewValues["JobDesc"] == null ? string.Empty : e.NewValues["JobDesc"].ToString();
            string _Achievement = e.NewValues["Achievement"] == null ? string.Empty : e.NewValues["Achievement"].ToString();
            string _LeaveReason = e.NewValues["LeaveReason"] == null ? string.Empty : e.NewValues["LeaveReason"].ToString();
            string _CompanySite = e.NewValues["CompanySite"] == null ? string.Empty : e.NewValues["CompanySite"].ToString();
            string _ContactName = e.NewValues["ContactName"] == null ? string.Empty : e.NewValues["ContactName"].ToString();
            string _ContactMobile = e.NewValues["ContactMobile"] == null ? string.Empty : e.NewValues["ContactMobile"].ToString();
            string _ContacttMail = e.NewValues["ContacttMail"] == null ? string.Empty : e.NewValues["ContacttMail"].ToString();

            DateTime? FromYear = e.NewValues["FromYear"] == null ? null : new DateTime?(Convert.ToDateTime(e.NewValues["FromYear"]));

            string toYearString = e.NewValues["ToYear"] == null ? null : e.NewValues["ToYear"].ToString();




            var entity = memoryList.Where(u => u.Item.Idx == key).FirstOrDefault();
            if (entity.Status == EditableItemStatus.Default)
                entity.Status = EditableItemStatus.Edit;


            DateTime toYear = DateTime.Now;
            if (!DateTime.TryParse(toYearString, out toYear)) {
                entity.Item.ToYear = null; ;
            }
            else {
                entity.Item.ToYear = toYear;
            }
            entity.Item.CompanyName = _CompanyName;
            entity.Item.Position = _Position;
            entity.Item.ReportTo = _ReportTo;
            entity.Item.CompanyAddress = _CompanyAddress;
            entity.Item.Achievement = _Achievement;
            entity.Item.LeaveReason = _LeaveReason;
            entity.Item.CompanySite = _CompanySite;
            entity.Item.ContactName = _ContactName;
            entity.Item.ContactMobile = _ContactMobile;
            entity.Item.ContacttMail = _ContacttMail;
            entity.Item.JobDesc = _JobDesc;
            entity.Item.FromYear = FromYear;

            entity.Item.ResumeId = p.ResumeIdx;

            Set(AspGridViewName, memoryList);
        }

        protected override void PreInserting(ResumeParam p, DevExpress.Web.Data.ASPxDataInsertingEventArgs e, DevExpress.Web.ASPxGridView.ASPxGridView sender) {
            var memoryList = Get<TL_User_CareerExperience>(u => true);

            if (memoryList == null) return;

            string _CompanyName = e.NewValues["CompanyName"] == null ? string.Empty : e.NewValues["CompanyName"].ToString();
            string _Position = e.NewValues["Position"] == null ? string.Empty : e.NewValues["Position"].ToString();
            string _ReportTo = e.NewValues["ReportTo"] == null ? string.Empty : e.NewValues["ReportTo"].ToString();
            string _CompanyAddress = e.NewValues["CompanyAddress"] == null ? string.Empty : e.NewValues["CompanyAddress"].ToString();
            string _JobDesc = e.NewValues["JobDesc"] == null ? string.Empty : e.NewValues["JobDesc"].ToString();
            string _Achievement = e.NewValues["Achievement"] == null ? string.Empty : e.NewValues["Achievement"].ToString();
            string _LeaveReason = e.NewValues["LeaveReason"] == null ? string.Empty : e.NewValues["LeaveReason"].ToString();
            string _CompanySite = e.NewValues["CompanySite"] == null ? string.Empty : e.NewValues["CompanySite"].ToString();
            string _ContactName = e.NewValues["ContactName"] == null ? string.Empty : e.NewValues["ContactName"].ToString();
            string _ContactMobile = e.NewValues["ContactMobile"] == null ? string.Empty : e.NewValues["ContactMobile"].ToString();
            string _ContacttMail = e.NewValues["ContacttMail"] == null ? string.Empty : e.NewValues["ContacttMail"].ToString();

            DateTime? FromYear = e.NewValues["FromYear"] == null ? null : new DateTime?(Convert.ToDateTime(e.NewValues["FromYear"]));
            string toYearString = e.NewValues["ToYear"] == null ? null : e.NewValues["ToYear"].ToString();


            EditableItem<TL_User_CareerExperience> entity = new EditableItem<TL_User_CareerExperience>();

            entity.Status = EditableItemStatus.Insert;
            entity.Item = new TL_User_CareerExperience();
            entity.Item.CompanyName = _CompanyName;
            entity.Item.Position = _Position;
            entity.Item.ReportTo = _ReportTo;
            entity.Item.CompanyAddress = _CompanyAddress;
            entity.Item.Achievement = _Achievement;
            entity.Item.LeaveReason = _LeaveReason;
            entity.Item.CompanySite = _CompanySite;
            entity.Item.ContactName = _ContactName;
            entity.Item.ContactMobile = _ContactMobile;
            entity.Item.ContacttMail = _ContacttMail;
            entity.Item.JobDesc = _JobDesc;
            entity.Item.FromYear = FromYear;
            DateTime toYear = DateTime.Now;
            if (!DateTime.TryParse(toYearString, out toYear)) {
                entity.Item.ToYear = null; ;
            }
            else {
                entity.Item.ToYear = toYear;
            }
            entity.Item.ResumeId = p.ResumeIdx;
            entity.Item.RowTime = DateTime.Now;
            if (memoryList.Count > 0)
                entity.Item.Idx = memoryList.Select(u => u.Item.Idx).Max() + 1;
            else
                entity.Item.Idx = 9999;

            memoryList.Add(entity);
            Set(AspGridViewName, memoryList);
        }
    }
}